package com.ruoyi.web.controller.admin;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.MenuGoods;
import com.ruoyi.system.service.IMenuGoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MerchantMenu;
import com.ruoyi.system.service.IMerchantMenuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商家菜单Controller
 * 
 * @author ruoyi
 * @date 2025-09-17
 */
@RestController
@RequestMapping("/system/menuMerchant")
public class MerchantMenuController extends BaseController
{
    @Autowired
    private IMerchantMenuService merchantMenuService;

    @Autowired
    private IMenuGoodsService menuGoodsService;

    /**
     * 查询商家菜单列表
     */
    @PreAuthorize("@ss.hasPermi('system:menuMerchant:list')")
    @GetMapping("/list")
    public TableDataInfo list(MerchantMenu merchantMenu)
    {
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        if (SecurityUtils.getUserId().intValue() != 1){
            for (SysRole role : roles){
                if (role.getRoleKey().equals("merchant")){
                    merchantMenu.setMerchantId(SecurityUtils.getLoginUser().getUser().getMerchantId());
                }
            }
        }
        startPage();
        List<MerchantMenu> list = merchantMenuService.selectMerchantMenuList(merchantMenu);
        return getDataTable(list);
    }

    /**
     * 获取商家菜单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:menuMerchant:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(merchantMenuService.selectMerchantMenuById(id));
    }

    /**
     * 新增商家菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menuMerchant:add')")
    @Log(title = "商家菜单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MerchantMenu merchantMenu)
    {
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        if (SecurityUtils.getUserId().intValue() != 1){
            for (SysRole role : roles){
                if (role.getRoleKey().equals("merchant")){
                    merchantMenu.setMerchantId(SecurityUtils.getLoginUser().getUser().getMerchantId());
                    merchantMenu.setUserId(SecurityUtils.getUserId());
                }
            }
        }
        return toAjax(merchantMenuService.insertMerchantMenu(merchantMenu));
    }

    /**
     * 修改商家菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menuMerchant:edit')")
    @Log(title = "商家菜单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MerchantMenu merchantMenu)
    {
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        if (SecurityUtils.getUserId().intValue() != 1){
            for (SysRole role : roles){
                if (role.getRoleKey().equals("merchant")){
                    merchantMenu.setMerchantId(SecurityUtils.getLoginUser().getUser().getMerchantId());
                }
            }
        }
        return toAjax(merchantMenuService.updateMerchantMenu(merchantMenu));
    }

    /**
     * 删除商家菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menuMerchant:remove')")
    @Log(title = "商家菜单", businessType = BusinessType.DELETE)
	@DeleteMapping("/remove")
    public AjaxResult remove(@RequestBody MerchantMenu merchantMenu)
    {
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        if (SecurityUtils.getUserId().intValue() != 1){
            for (SysRole role : roles){
                if (role.getRoleKey().equals("merchant")){
                    merchantMenu.setMerchantId(SecurityUtils.getLoginUser().getUser().getMerchantId());
                }
            }
        }
        return toAjax(merchantMenuService.deleteMerchantMenu(merchantMenu));
    }

    /**
     * 新增商品信息关联
     *
     */
    @PreAuthorize("@ss.hasPermi('system:menuMerchant:addMenuGoods')")
    @Log(title = "商家菜单", businessType = BusinessType.INSERT)
    @PostMapping("/addGoods")
    public AjaxResult addGoods(@RequestBody List<MenuGoods> menuGoods)
    {
        return toAjax(menuGoodsService.addMenuGoodsList(menuGoods));
    }

    /**
     * 解除商品信息和菜单关联
     */
    @PreAuthorize("@ss.hasPermi('system:menuMerchant:removeMenuGoods')")
    @Log(title = "商家菜单", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeMenuGoods")
    public AjaxResult removeMenuGoods(@RequestBody MenuGoods menuGoods)
    {
        return toAjax(menuGoodsService.deleteMenuGoods(menuGoods));
    }

}
